package com.lxq.test1803.main01;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int flag,ans = 0,x=0;
        do {
            flag = sc.nextInt();
            if (flag==1){
                ans++;
                x=0;
            }
            if (flag==2){
                x++;
                ans = ans + 2*x;
            }
            if (flag==0){
                System.out.println(ans);
            }
        }while (flag!=0);
    }
}
